﻿namespace FilterDemo
{
	using System;
	using System.Collections.Generic;
	using System.Diagnostics;
	using System.Windows.Forms;

	using PugLib;
	using PugLib.Collections;
	using PugLib.Filters;
	using PugLib.TestCollateral;
	using PugLib.UI;
	using PugLib.UI.Controls;

	internal sealed class FilterDemoController
	{
		private static readonly string[] _fieldsForDemo =
			new[] { "Language", "Number", "Domain", "Name", "Operating System", "Boot Date", "IP Address" };

		private readonly NotifyChanged<List<SearchFilter>> _currentFilters;
		private readonly SortableSearchableCollection<Computer> _dataForDemo;
		private readonly SearchFilterGroup _searchGroup;
		private readonly FilterDemoView _view;

		internal FilterDemoController(FilterDemoView demo)
		{
			_view = demo;

			_dataForDemo = new SortableSearchableCollection<Computer>();

			_searchGroup = new SearchFilterGroup();
			_searchGroup.SetFields(_fieldsForDemo);

			_currentFilters = new NotifyChanged<List<SearchFilter>>();
			_currentFilters.StateChanged += FiltersChanged;
		}

		internal SortableSearchableCollection<Computer> DataForDemo
		{
			get { return _dataForDemo; }
		}

		internal SearchFilterGroup SearchGroup
		{
			get { return _searchGroup; }
		}

		private void FiltersChanged(object sender, StateChangedEventArgs<List<SearchFilter>> e)
		{
			_searchGroup.SetFilters(e.State);

			_view.UpdateFilterView(e.State);
		}

		internal void SetCurrentFilters(List<SearchFilter> list)
		{
			_currentFilters.State = list;
		}

		internal void LaunchFilterBuilder()
		{
			using (FilterBuilder builder = new FilterBuilder())
			{
				builder.SetState(SearchGroup);
				DialogResult result = builder.ShowDialog();

				if (result == DialogResult.Cancel)
				{
					return;
				}

				SetCurrentFilters(new List<SearchFilter>(builder.Filters));

				_view.UpdateSearchingView();
			}
		}

		internal void GenerateTestState()
		{
			DataForDemo.Add(new Computer
			                {
			                	Domain = null,
			                	Name = null,
			                	OperatingSystem = null,
			                	BootDate = null,
			                	IPAddress = null,
			                	Number = null,
			                	Language = null
			                });

			const int count = 500000;
			Repeat.Call(DataForDemo.Add, Computer.CreateRandom, count);

			Debug.Assert(DataForDemo.Count == (count + 1)); // 1 null above
		}


		internal IList<Computer> GetFilteredItems()
		{
			return SearchFilterQuery.Execute(DataForDemo, SearchGroup);
		}

		internal static void ShowTestDialogs()
		{
			const string title = "PugLib MessageBox Demo";
			const string message =
				"Some think Windows is too inconsistent and creates bad habits.  Do you think this is true?";

			Console.WriteLine("ShowTestDialogs: " +
			                  PugMessageBox.Show(message, title, PugDialogButtons.YesNoCancel, PugDialogIcon.Question));
		}

		internal static void ShowCustomDialogTest()
		{
			const string title = "Custom MessageBox Demo";
			const string message =
				"Windows has created bad user assumptions.  Dialog boxes should have meaningful button labels.  What do you think?";

			Console.WriteLine("ShowCustomDialogTest: " +
			                  CustomMessageBox.Show(message, title, Images.Help, new[] { "YA RLY", "SRSLY", "Whatever", "Take off, hoser" }));
		}

		internal static void ShowCustomDialogExtraTest()
		{
			const string title = "Custom MessageBox Demo";
			const string message =
				"Windows has created bad user assumptions.  Dialog boxes should have meaningful button labels.\n\nWhat do you think?";

			List<Tuple<string, string>> buttons = new List<Tuple<string, string>>
			                                      {
			                                      	new Tuple<string, string>("YA RLY", "Cheezburger!"),
			                                      	new Tuple<string, string>("SRSLY", "Ham is good!"),
			                                      	new Tuple<string, string>("Whatever", "Lame dialog is lame"),
			                                      	new Tuple<string, string>("Take off,", "you hoser")
			                                      };

			Console.WriteLine("ShowCustomDialogExtraTest: " +
			                  CustomMessageBox.Show(message, title, Images.Help, buttons));
		}

		internal static void ShowTaskDialogTest()
		{
			const string title = "Custom MessageBox Demo";
			const string message =
				"Windows has created bad user assumptions.  Dialog boxes should have meaningful button labels.";
			const string content = "What do you think?";
			TaskDialog taskDialog = new TaskDialog(title, message, content);

			TaskDialogButton a = new TaskDialogButton { ButtonId = 101, ButtonText = "YA RLY\nCheezburger!" };
			TaskDialogButton b = new TaskDialogButton { ButtonId = 102, ButtonText = "SRSLY\nHam is good!" };
			TaskDialogButton c = new TaskDialogButton { ButtonId = 103, ButtonText = "Whatever\nLame dialog is lame" };
			TaskDialogButton d = new TaskDialogButton { ButtonId = 104, ButtonText = "Take off,\nyou hoser" };

			taskDialog.Buttons = new[] { a, b, c, d };
			Console.WriteLine("ShowCustomDialogExtraTest: " + taskDialog.Show());
		}
	}
}